package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FillerhazardController")
public class FillerhazardController extends CmiEmaController {

	@RequestMapping("/listFillerhazards")
	public ModelAndView listFillerhazards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = fillerhazardPaginationService.updatePagination(request, pagination);
		Set<Fillerhazard> fillerhazards = fillerhazardPaginationService.getPaginationFillerhazards(pagination);

		mav.addObject("fillerhazards", fillerhazards);
		mav.addObject("entityName", "fillerhazard");
		mav.addObject("pagination", pagination);
		mav.setViewName("fillerhazard/listFillerhazards.jsp");
		return mav;
	}

	public String listFillerhazards() {
		return "redirect:/listFillerhazards";
	}

	@RequestMapping("/viewFillerhazard")
	public ModelAndView viewFillerhazard(@RequestParam Integer fillerhazardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("fillerhazard", fillerhazardDAO.findFillerhazardByPrimaryKey(fillerhazardId));
		mav.setViewName("fillerhazard/viewFillerhazard.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFillerhazards")
	public ModelAndView confirmDeleteFillerhazards(
			@RequestParam(value = "deleteFillerhazardsIds", required = true) Set<Integer> deleteFillerhazardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Fillerhazard> deleteFillerhazards = fillerhazardDAO.findFillerhazardsByIds(deleteFillerhazardsIds);
		Set<Fillerhazard> referencedFillerhazards = fillerhazardService.loadReferenceFillerhazards(deleteFillerhazards);
		boolean hasReferenced;
		if (referencedFillerhazards.size() == 0) {
			mav.addObject("deleteEntitys", deleteFillerhazards);
			hasReferenced = false;
		} else {
			deleteFillerhazards.removeAll(referencedFillerhazards);
			mav.addObject("referencedEntitys", referencedFillerhazards);
			mav.addObject("deleteEntitys", deleteFillerhazards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Fillerhazard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFillerhazards");
		return mav;
	}

	@RequestMapping("/deleteFillerhazard")
	public String deleteFillerhazard(@RequestParam Integer fillerhazardId) {
		Fillerhazard fillerhazard = fillerhazardDAO.findFillerhazardByPrimaryKey(fillerhazardId);
		fillerhazardService.deleteFillerhazard(fillerhazard);
		return "forward:/listFillerhazards";
	}

	@RequestMapping("/confirmDeleteFillerhazard")
	public ModelAndView confirmDeleteFillerhazard(@RequestParam Integer fillerhazardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("fillerhazard", fillerhazardDAO.findFillerhazardByPrimaryKey(fillerhazardId));

		mav.setViewName("fillerhazard/deleteFillerhazard.jsp");

		return mav;
	}

	@RequestMapping("/deleteFillerhazards")
	public String deleteFillerhazards(
			@RequestParam(value = "deleteFillerhazardsId", required = true) Set<Integer> deleteFillerhazardsId) {
		fillerhazardService.deleteFillerhazards(deleteFillerhazardsId);
		return "forward:/listFillerhazards";
	}

	@RequestMapping("/editFillerhazard")
	public ModelAndView editFillerhazard(@RequestParam Integer fillerhazardId) {

		ModelAndView mav = new ModelAndView();

		Fillerhazard fillerhazard = fillerhazardDAO.findFillerhazardByPrimaryKey(fillerhazardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("fillerhazard", fillerhazard);

		// 把所有的引用fillerhazard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(fillerhazardService.loadFillerhazardObjectReferencedSet(fillerhazard));

		// 将编辑页面fillerhazard添加到mav中
		mav.setViewName("fillerhazard/editFillerhazard.jsp");
		return mav;
	}

	@RequestMapping("/newFillerhazard")
	public ModelAndView newFillerhazard() {
		ModelAndView mav = new ModelAndView();
		Fillerhazard fillerhazard = new Fillerhazard();

		mav.addObject("fillerhazard", fillerhazard);

		// 把所有的引用fillerhazard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(fillerhazardService.loadFillerhazardObjectReferencedSet(fillerhazard));

		mav.addObject("newFlag", true);
		mav.setViewName("fillerhazard/editFillerhazard.jsp");

		return mav;
	}

	@RequestMapping("/saveFillerhazard")
	public ModelAndView saveFillerhazard(@ModelAttribute Fillerhazard fillerhazard) {

		ModelAndView mav = new ModelAndView();

		Filler fillerTemp = fillerhazard.getFiller();
		Filler filler = fillerTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(fillerDAO.findFillerById(fillerTemp.getId()));
		Hazard hazardTemp = fillerhazard.getHazard();
		Hazard hazard = hazardTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(hazardDAO.findHazardById(hazardTemp.getId()));

		Fillerhazard existingFillerhazard = CmiHibernateProxyUtil.deproxy(fillerhazardDAO
				.findFillerhazardByPhysicalPrimaryKey(filler.getId(), hazard.getId(), fillerhazard.getDegree()));

		fillerhazard.setFiller(filler);
		fillerhazard.setHazard(hazard);
		// 新建记录
		if (fillerhazard.getId() == null) {
			if (fillerhazard.equals(existingFillerhazard)) {

				mav.addAllObjects(fillerhazardService.loadFillerhazardClassReferencedSet(fillerhazard));
				// mav = editContinue(mav, fillerhazard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("fillerhazard/editFillerhazard.jsp");
			} else {
				fillerhazardService.saveFillerhazard(fillerhazard);
				mav.setViewName("forward:/listFillerhazards");
			}
		}
		// 编辑记录
		else {
			Fillerhazard originFillerhazard = CmiHibernateProxyUtil
					.deproxy(fillerhazardDAO.findFillerhazardByPrimaryKey(fillerhazard.getId()));
			if (!fillerhazard.equals(originFillerhazard) && fillerhazard.equals(existingFillerhazard)) {
				mav.addAllObjects(fillerhazardService.loadFillerhazardClassReferencedSet(fillerhazard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("fillerhazard/editFillerhazard.jsp");
			} else {
				fillerhazardService.saveFillerhazard(fillerhazard);
				mav.setViewName("forward:/listFillerhazards");
			}
		}
		return mav;
	}

	@RequestMapping("/fillerhazardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
